import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'
import { ElMessage } from "element-plus";


const routes = [
  {
    path: '/',
    name: 'home',
    component: HomeView,
    redirect:'/index',
    children:[
      {path: '/index', component: () => import('../views/index/IndexView.vue')},
      {path: '/employee', component: () => import('../views/employee/EmployeeView.vue')},
      {path: '/dict', component: () => import('../views/dictionary/DictView.vue')},
      {path: '/dictOption', component: () => import('../views/dictionary/DictOptionView.vue')},
      {path: '/elderly', component: () => import('../views/elderly/ElderlyView.vue')},
      {path: '/goods', component: () => import('../views/goods/DailyNecessities.vue')},
      {path: '/healthe', component: () => import('../views/healthe/OlderHealthe.vue')},
      {path: '/amount', component: () => import('../views/healthe/Amount.vue')},
      {path: '/order', component: () => import('../views/order/OrderView.vue')},
      {path: '/balance', component: () => import('../views/order/OrderBalance.vue')},
      {path: '/bed', component: () => import('../views/checkin/Bed.vue')},
      {path: '/belongings', component: () => import('../views/checkin/Belongings.vue')},
      {path: '/checkinrecord', component: () => import('../views/checkin/Record.vue')},
      {path: '/excused', component: () => import('../views/checkin/Excused.vue')},
      {path: '/notice',component: () => import('../views/notice/NoticeView.vue')}
      {path: '/healthCounse', component: () => import('../views/health-counse/HealthCounse.vue')},
    ]
  },
  {path: '/login', component: () => import('../views/LoginView.vue')}
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})

// 添加全局路由守卫
router.beforeEach((to, from, next) => {
  const isAuthenticated = localStorage.getItem("user"); // 检查用户登录状态
  if (to.path !== "/login" && !isAuthenticated) {
    ElMessage({
      message: "请先登录后再访问其他页面",
      type: "warning",
      duration: 3000, // 消息显示时间（毫秒）
    });
    next("/login"); // 未登录跳转到登录页面
  } else {
    next(); // 继续导航
  }
});
export default router
